package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.PrivateMessage;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.PrivateMessageMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IPrivateMessageService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PrivateMessageServiceImpl implements IPrivateMessageService {
    @Autowired
    private PrivateMessageMapper privateMessageMapper;

   /* @Override
    public void saveSend(PrivateMessage privateMessage) {
        User user = UserContext.getCurrentUser();
        privateMessage.setUserId(user.getId());
        privateMessage.setSenderId(user.getId());
        privateMessage.setMessageType(1L);
        privateMessage.setSendTime(new Date());
        privateMessage.setStatus(2L);
        privateMessageMapper.insert(privateMessage);
    }*/

    @Override
    public void saveReceiver(PrivateMessage privateMessage) {
        User user = UserContext.getCurrentUser();
        privateMessage.setFriendId(user.getId());
        privateMessage.setSenderId(user.getId());
        privateMessage.setMessageType(1L);
        privateMessage.setSendTime(new Date());
        privateMessage.setStatus(1L);
        privateMessageMapper.insert(privateMessage);
    }

    @Override
    public Integer queryPrivateMessageCount(Long uid) {
        Integer count = privateMessageMapper.listPrivateMessageCountByStatus(uid);
        return count;
    }

    @Override
    public void changeStatus(Long uid) {
        privateMessageMapper.changeStatus(uid);
    }

    @Override
    public PageInfo<PrivateMessage> query(QueryObject qo) {
        User currentUser = UserContext.getCurrentUser();
        qo.setUserId(currentUser.getId());
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"pm.send_time desc");
        List<PrivateMessage> list = privateMessageMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public PrivateMessage queryOneForId(Long id) {
        PrivateMessage privateMessage = privateMessageMapper.selectOneForId(id);
        return privateMessage;
    }

    @Override
    public void deleteLetter(Long id) {
        privateMessageMapper.deleteByPrimaryKey(id);
    }
}
